<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>imageloader.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_net_imageloader.js.html">imageloader.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2008 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Image loader utility class.  Useful when an application needs
<a name="line17"></a> * to preload multiple images, for example so they can be sized.
<a name="line18"></a> *
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.net.ImageLoader&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.dom&#39;);
<a name="line24"></a>goog.require(&#39;goog.events.EventHandler&#39;);
<a name="line25"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line26"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line27"></a>goog.require(&#39;goog.net.EventType&#39;);
<a name="line28"></a>goog.require(&#39;goog.object&#39;);
<a name="line29"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line30"></a>
<a name="line31"></a>/**
<a name="line32"></a> * Image loader utility class.  Raises a {@link goog.events.EventType.LOAD}
<a name="line33"></a> * event for each image loaded, with an {@link Image} object as the target of
<a name="line34"></a> * the event, normalized to have {@code naturalHeight} and {@code naturalWidth}
<a name="line35"></a> * attributes.
<a name="line36"></a> * @param {Element} opt_parent An optional parent element whose document object
<a name="line37"></a> *     should be used to load images.
<a name="line38"></a> * @constructor
<a name="line39"></a> * @extends {goog.events.EventTarget}
<a name="line40"></a> */
<a name="line41"></a>goog.net.ImageLoader = function(opt_parent) {
<a name="line42"></a>  goog.events.EventTarget.call(this);
<a name="line43"></a>  this.images_ = {};
<a name="line44"></a>  this.handler_ = new goog.events.EventHandler(this);
<a name="line45"></a>  this.parent_ = opt_parent;
<a name="line46"></a>};
<a name="line47"></a>goog.inherits(goog.net.ImageLoader, goog.events.EventTarget);
<a name="line48"></a>
<a name="line49"></a>
<a name="line50"></a>/**
<a name="line51"></a> * Map of image IDs to images, used to keep track of the images to load.
<a name="line52"></a> * @private
<a name="line53"></a> * @type {Object}
<a name="line54"></a> */
<a name="line55"></a>goog.net.ImageLoader.prototype.images_;
<a name="line56"></a>
<a name="line57"></a>
<a name="line58"></a>/**
<a name="line59"></a> * Event handler object, used to keep track of onload and onreadystatechange
<a name="line60"></a> * listeners.
<a name="line61"></a> * @private
<a name="line62"></a> * @type {goog.events.EventHandler}
<a name="line63"></a> */
<a name="line64"></a>goog.net.ImageLoader.prototype.handler_;
<a name="line65"></a>
<a name="line66"></a>
<a name="line67"></a>/**
<a name="line68"></a> * The parent element whose document object will be used to load images.
<a name="line69"></a> * Useful if you want to load the images from a window other than the current
<a name="line70"></a> * window in order to control the Referer header sent when the image is loaded.
<a name="line71"></a> * @type {(Element|undefined)}
<a name="line72"></a> * @private
<a name="line73"></a> */
<a name="line74"></a>goog.net.ImageLoader.prototype.parent_;
<a name="line75"></a>
<a name="line76"></a>
<a name="line77"></a>/**
<a name="line78"></a> * Adds an image to the image loader, and associates it with the given ID
<a name="line79"></a> * string.  If an image with that ID already exists, it is silently replaced.
<a name="line80"></a> * When the image in question is loaded, the target of the LOAD event will be
<a name="line81"></a> * an {@code Image} object with {@code id} and {@code src} attributes based on
<a name="line82"></a> * these arguments.
<a name="line83"></a> * @param {string} id The ID of the image to load.
<a name="line84"></a> * @param {string|Image} image Either the source URL of the image or the HTML
<a name="line85"></a> *     image element itself (or any object with a {@code src} property, really).
<a name="line86"></a> */
<a name="line87"></a>goog.net.ImageLoader.prototype.addImage = function(id, image) {
<a name="line88"></a>  var src = goog.isString(image) ? image : image.src;
<a name="line89"></a>  if (src) {
<a name="line90"></a>    // For now, we just store the source URL for the image.
<a name="line91"></a>    this.images_[id] = src;
<a name="line92"></a>  }
<a name="line93"></a>};
<a name="line94"></a>
<a name="line95"></a>
<a name="line96"></a>/**
<a name="line97"></a> * Removes the image associated with the given ID string from the image loader.
<a name="line98"></a> * @param {string} id The ID of the image to remove.
<a name="line99"></a> */
<a name="line100"></a>goog.net.ImageLoader.prototype.removeImage = function(id) {
<a name="line101"></a>  goog.object.remove(this.images_, id);
<a name="line102"></a>};
<a name="line103"></a>
<a name="line104"></a>
<a name="line105"></a>/**
<a name="line106"></a> * Starts loading all images in the image loader in parallel.  Raises a LOAD
<a name="line107"></a> * event each time an image finishes loading, and a COMPLETE event after all
<a name="line108"></a> * images have finished loading.
<a name="line109"></a> */
<a name="line110"></a>goog.net.ImageLoader.prototype.start = function() {
<a name="line111"></a>  goog.object.forEach(this.images_, this.loadImage_, this);
<a name="line112"></a>};
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>/**
<a name="line116"></a> * Creates an {@code Image} object with the specified ID and source URL, and
<a name="line117"></a> * listens for network events raised as the image is loaded.
<a name="line118"></a> * @private
<a name="line119"></a> * @param {string} src The image source URL.
<a name="line120"></a> * @param {string} id The unique ID of the image to load.
<a name="line121"></a> */
<a name="line122"></a>goog.net.ImageLoader.prototype.loadImage_ = function(src, id) {
<a name="line123"></a>  var image;
<a name="line124"></a>  if (this.parent_) {
<a name="line125"></a>    var dom = goog.dom.getDomHelper(this.parent_);
<a name="line126"></a>    image = dom.createDom(&#39;img&#39;);
<a name="line127"></a>  } else {
<a name="line128"></a>    image = new Image();
<a name="line129"></a>  }
<a name="line130"></a>
<a name="line131"></a>  // Semi-hack:  We used to map the ID string to the source URL string, but now
<a name="line132"></a>  // we use the same object to map the ID string to the Image object itself.
<a name="line133"></a>  this.images_[id] = image;
<a name="line134"></a>
<a name="line135"></a>  // Internet Explorer doesn&#39;t reliably raise LOAD events on images, so we must
<a name="line136"></a>  // use READY_STATE_CHANGE (thanks, Jeff!).
<a name="line137"></a>  // If the image is cached locally, IE won&#39;t fire the LOAD event while the
<a name="line138"></a>  // onreadystate event is fired always. On the other hand, the ERROR event
<a name="line139"></a>  // is always fired whenever the image is not loaded successfully no matter
<a name="line140"></a>  // whether it&#39;s cached or not.
<a name="line141"></a>
<a name="line142"></a>  var loadEvent = goog.userAgent.IE ? goog.net.EventType.READY_STATE_CHANGE :
<a name="line143"></a>      goog.events.EventType.LOAD;
<a name="line144"></a>  this.handler_.listen(image, [
<a name="line145"></a>    loadEvent, goog.net.EventType.ABORT, goog.net.EventType.ERROR
<a name="line146"></a>  ], this.onNetworkEvent_);
<a name="line147"></a>
<a name="line148"></a>  image.id = id;
<a name="line149"></a>  image.src = src;
<a name="line150"></a>};
<a name="line151"></a>
<a name="line152"></a>
<a name="line153"></a>/**
<a name="line154"></a> * Handles net events (READY_STATE_CHANGE, LOAD, ABORT, and ERROR).
<a name="line155"></a> * @private
<a name="line156"></a> * @param {goog.events.Event} evt The network event to handle.
<a name="line157"></a> */
<a name="line158"></a>goog.net.ImageLoader.prototype.onNetworkEvent_ = function(evt) {
<a name="line159"></a>  var image = evt.currentTarget;
<a name="line160"></a>
<a name="line161"></a>  if (!image) {
<a name="line162"></a>    return;
<a name="line163"></a>  }
<a name="line164"></a>
<a name="line165"></a>  if (evt.type == goog.net.EventType.READY_STATE_CHANGE) {
<a name="line166"></a>    // This implies that the user agent is IE; see loadImage()_.
<a name="line167"></a>    // Noe that this block is used to check whether the image is ready to
<a name="line168"></a>    // dispatch the COMPLETE event.
<a name="line169"></a>    if (image.readyState == goog.net.EventType.COMPLETE) {
<a name="line170"></a>      // This is the IE equivalent of a LOAD event.
<a name="line171"></a>      evt.type = goog.events.EventType.LOAD;
<a name="line172"></a>    } else {
<a name="line173"></a>      // This may imply that the load failed.
<a name="line174"></a>      // Note that the image has only the following states:
<a name="line175"></a>      //   * uninitialized
<a name="line176"></a>      //   * loading
<a name="line177"></a>      //   * complete
<a name="line178"></a>      // When the ERROR or the ABORT event is fired, the readyState
<a name="line179"></a>      // will be either uninitialized or loading and we&#39;d ignore those states
<a name="line180"></a>      // since they will be handled separately (eg: evt.type = &#39;ERROR&#39;).
<a name="line181"></a>
<a name="line182"></a>      // Notes from MSDN : The states through which an object passes are
<a name="line183"></a>      // determined by that object. An object can skip certain states
<a name="line184"></a>      // (for example, interactive) if the state does not apply to that object.
<a name="line185"></a>      // see http://msdn.microsoft.com/en-us/library/ms534359(VS.85).aspx
<a name="line186"></a>
<a name="line187"></a>      // The image is not loaded, ignore.
<a name="line188"></a>      return;
<a name="line189"></a>    }
<a name="line190"></a>  }
<a name="line191"></a>
<a name="line192"></a>  // Add natural width/height properties for non-Gecko browsers.
<a name="line193"></a>  if (typeof image.naturalWidth == &#39;undefined&#39;) {
<a name="line194"></a>    if (evt.type == goog.events.EventType.LOAD) {
<a name="line195"></a>      image.naturalWidth = image.width
<a name="line196"></a>      image.naturalHeight = image.height;
<a name="line197"></a>    } else {
<a name="line198"></a>      // This implies that the image fails to be loaded.
<a name="line199"></a>      image.naturalWidth = 0;
<a name="line200"></a>      image.naturalHeight = 0;
<a name="line201"></a>    }
<a name="line202"></a>  }
<a name="line203"></a>
<a name="line204"></a>  // Redispatch the event on behalf of the image.
<a name="line205"></a>  this.dispatchEvent({type: evt.type, target: image});
<a name="line206"></a>
<a name="line207"></a>  // Remove the image from the map.
<a name="line208"></a>  goog.object.remove(this.images_, image.id);
<a name="line209"></a>
<a name="line210"></a>  // If this was the last image, raise a COMPLETE event.
<a name="line211"></a>  if (goog.object.isEmpty(this.images_)) {
<a name="line212"></a>    this.dispatchEvent(goog.net.EventType.COMPLETE);
<a name="line213"></a>    // Unlisten for all network events.
<a name="line214"></a>    if (this.handler_) {
<a name="line215"></a>      this.handler_.removeAll();
<a name="line216"></a>    }
<a name="line217"></a>  }
<a name="line218"></a>};
<a name="line219"></a>
<a name="line220"></a>
<a name="line221"></a>/**
<a name="line222"></a> * Disposes of the image loader.
<a name="line223"></a> */
<a name="line224"></a>goog.net.ImageLoader.prototype.disposeInternal = function() {
<a name="line225"></a>  if (this.images_) {
<a name="line226"></a>    delete this.images_;
<a name="line227"></a>  }
<a name="line228"></a>  if (this.handler_) {
<a name="line229"></a>    this.handler_.dispose();
<a name="line230"></a>    this.handler_ = null;
<a name="line231"></a>  }
<a name="line232"></a>  goog.net.ImageLoader.superClass_.disposeInternal.call(this);
<a name="line233"></a>};
</pre>


</body>
</html>
